package top.yangbuyi.controller.doctor;

import cn.hutool.core.bean.BeanUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.controller.BaseController;
import top.yangbuyi.domain.*;
import top.yangbuyi.dto.PatientDto;
import top.yangbuyi.service.CareService;
import top.yangbuyi.service.PatientService;
import top.yangbuyi.vo.AjaxResult;
import top.yangbuyi.vo.DataGridView;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @description: 杨不易网站:www.yangbuyi.top
 * @program: yangbuyiopenHis2020
 * @ClassName: PatientController
 * @create: 2020-09-15 10:22
 * @author: yangbuyi
 * @since： JDK1.8
 * @PatientController: 患者控制器$
 **/

@RestController
@Slf4j
@RequestMapping("doctor/patient")
public class PatientController extends BaseController {

	  /**
	   * @功能描述:患者信息和患者详细详细远程调用
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 23:54
	   */
	  @Reference
	  private PatientService patientService;
	  /**
	   * @功能描述:调用就诊的患者病例详细远程调用
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 23:54
	   */
	  @Reference
	  private CareService careService;

	  /**
	   * @功能描述:分页查询所有患者信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 10:24
	   */
	  @HystrixCommand
	  @GetMapping("listPatientForPage")
	  public AjaxResult listPatientForPage(PatientDto patientDto) {
			DataGridView dataGridView = this.patientService.listPatientForPage(patientDto);
			return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
	  }

	  /**
	   * @功能描述:根据患者ID查询患者信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 10:30
	   */
	  @HystrixCommand
	  @GetMapping("getPatientById/{patientId}")
	  public AjaxResult getPatientById(@PathVariable @NotNull(message = "患者ID不允许为空") String patientId) {
			Patient patient = this.patientService.getPatientById(patientId);
			return AjaxResult.success(patient);
	  }

	  /**
	   * @功能描述:根据患者ID查询患者的档案信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 10:30
	   */
	  @HystrixCommand
	  @GetMapping("getPatientFileById/{patientId}")
	  public AjaxResult getPatientFileById(@PathVariable @NotNull(message = "患者ID不允许为空") String patientId) {
			PatientFile patientFile = this.patientService.getPatientFileById(patientId);
			return AjaxResult.success(patientFile);
	  }

	  /**
	   * @功能描述:根据患者ID查询患者所有信息【基础，档案，病例】---已完成
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 0：19
	   */
	  @HystrixCommand
	  @GetMapping("getPatientAllMessageByPatientId/{patientId}")
	  public AjaxResult getPatientAllMessageByPatientId(@PathVariable @NotNull(message = "患者ID不允许为空") String patientId) {
			// 根据患者ID 查询患者病例信息
			List<CareHistory> careHistories = this.careService.queryCareHistoryByPatientId(patientId);
			// 构造返回数据
			List<Map<String, Object>> res = new ArrayList<>();
			// 遍历开始
			for (CareHistory careHistory : careHistories) {
				  // 将每一个 病例信息转换map
				  Map<String, Object> map = BeanUtil.beanToMap(careHistory);
				  System.err.println(map);
				  // 1. 构造处方信息
				  map.put("careOrders", Constants.EMPTY_LIST);
				  // 1.1 构造组装 病例 和 处方信息
				  List<Map<String, Object>> reCareOrders = new ArrayList<>();
				  // 1.2 根据病例ID 查询处方信息
				  List<CareOrder> careOrders = this.careService.queryCareOrdersByChId(careHistory.getChId());
				  // 2. 根据处方ID 查询处方详细信息
				  for (CareOrder careOrder : careOrders) {
						// 2.2 转换每一个处方对象 （json串）
						Map<String, Object> careOrderMap = BeanUtil.beanToMap(careOrder);
						// 2.3 根据处方ID 查询处方详细信息
						List<CareOrderItem> careOrderItems = this.careService.queryCareOrdersItemsByCoId(careOrder.getCoId(), null);
						// 2.4 将查询到的处方详细详细 注入到careOrderMap
						careOrderMap.put("careOrderItems", careOrderItems);
						// 3. 添加到集合当中
						reCareOrders.add(careOrderMap);
				  }
				  // 返回组装好的 病例  病例详情 处方  处方详情
				  map.put("careOrders", reCareOrders);
				  res.add(map);
			}
			// 遍历结束

			return AjaxResult.success(res);
	  }

}
